/**
 * Copyright (C) 2010 - 2013 Harry Glasgow
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.jaden.ui.dialog.projectdefinition;

import com.googlecode.jaden.common.config.IOConfiguration;
import com.googlecode.jaden.common.other.Utils;
import com.googlecode.jaden.ui.UiUtils;
import static com.googlecode.jaden.ui.Constants.*;
import com.googlecode.jaden.ui.dialog.BaseDialog;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.util.prefs.Preferences;

public class EditNumericIODialog extends BaseDialog {

    private SpinnerNumberModel minimumInputModel;
    private SpinnerNumberModel maximumInputModel;

    private IOConfiguration configuration;

    public EditNumericIODialog(EditProjectDefinitionDialog dialog, IOConfiguration configuration, Preferences preferences) {
        super(dialog, DIALOG_PROJECT_DETAILS_NUMERIC_TITLE);
        this.configuration = configuration;

        JLabel minimumLabel = new JLabel(Utils.translate(LABEL_INPUT_MIN_TEXT));
        minimumInputModel = new SpinnerNumberModel(0, -999999, 999999, 1);
        JSpinner minimumInputSpinner = new JSpinner(minimumInputModel);

        JLabel maximumLabel = new JLabel(Utils.translate(LABEL_INPUT_MAX_TEXT));
        maximumInputModel = new SpinnerNumberModel(0, -999999, 999999, 1);
        JSpinner maximumInputSpinner = new JSpinner(maximumInputModel);

        JPanel mainPanel = UiUtils.verticalList(UiUtils.horizontalList(minimumLabel, minimumInputSpinner),
                UiUtils.horizontalList(maximumLabel, maximumInputSpinner));
        setMainPanel(mainPanel, preferences);

        UiUtils.enWidth(minimumLabel, maximumLabel);

        minimumInputModel.setValue(configuration.getNumericMinValue());
        maximumInputModel.setValue(configuration.getNumericMaxValue());

        minimumInputModel.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int min = minimumInputModel.getNumber().intValue();
                int max = maximumInputModel.getNumber().intValue();
                if (max <= min) {
                    minimumInputModel.setValue(max - 1);
                }
            }
        });

        maximumInputModel.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int min = minimumInputModel.getNumber().intValue();
                int max = maximumInputModel.getNumber().intValue();
                if (max <= min) {
                    maximumInputModel.setValue(min + 1);
                }
            }
        });
    }

    protected void doOk() {
        configuration.setNumericMinValue(minimumInputModel.getNumber().intValue());
        configuration.setNumericMaxValue(maximumInputModel.getNumber().intValue());
        dispose();
    }

}